package com.finance.cooperate.common.utils;

import com.alibaba.fastjson.JSON;
import com.finance.cooperate.common.constant.Constant;
import org.apache.commons.jexl3.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName Jexl3Utils
 * @Description TODO
 * @Author shenanxin
 * @Date 2023/9/18 12:24
 * @Modify ...
 */
public class Jexl3Utils {


    /**
    * @Author shenanxin
    * @Description 解析需要的特征
    * @Date 12:25 2023/9/18
    * @Param [expression]
    * @return java.util.List<java.lang.String>
    **/
    public static List<String> extractVariables(String expression) {
        List<String> variables = new ArrayList<>();
        Pattern pattern = Pattern.compile("\\b\\w+\\b"); // 匹配单词边界内的单词字符
        Matcher matcher = pattern.matcher(expression);

        while (matcher.find()) {
            String variable = matcher.group();
            if (Character.isLetter(variable.charAt(0)) && !variable.equals(variable.toUpperCase())) {
                variables.add(variable);
            }
        }

        return variables;
    }


    /**
    * @Author shenanxin
    * @Description 判断是否符合条件
    * @Date 13:00 2023/9/18
    * @Param [condition, featureMap]
    * @return boolean
    **/
    public static boolean evaluateCondition(String condition, Map<String, Object> featureMap) {
        JexlEngine jexlEngine = new JexlBuilder().create();
        JexlContext jexlContext = new MapContext();

        // 将特征值添加到JexlContext
        for (Map.Entry<String, Object> entry : featureMap.entrySet()) {
            jexlContext.set(entry.getKey(), entry.getValue());
        }

        // 创建Jexl表达式对象并评估条件
        JexlExpression jexlExpression = jexlEngine.createExpression(condition);
        Object result = jexlExpression.evaluate(jexlContext);

        // 将结果转换为布尔值
        if (result instanceof Boolean) {
            return (Boolean) result;
        }

        // 默认情况下返回false
        return false;
    }



    public static void main(String[] args) {
//        String expression = "(amount6 < 2000.5 && exter_user_risk_lev_UPPER == 3) || (fdc_level =~ [1, 2, 6])";
//        List<String> variableNames = extractVariables(expression);
//
//        System.out.println(variableNames);

        // ['请求成功', '啦啦', '吧啦吧啦'].contains(text)

//        String expression = "[3.0, 4].contains(type_1)";

//        String expression = "f_identity_birthday_age <= 25 && f_personal_highest_school_record =~ [3, 4.0]";
        String expression = "f_scrapy_apps_finance_count == 1";

        Map<String, String> featureMap = new HashMap<>();
//        featureMap.put("f_identity_birthday_age", "24");
//        featureMap.put("f_personal_highest_school_record", "4.0");
        featureMap.put("f_scrapy_apps_finance_count", "1");


        Map<String, Object> feature = new HashMap<>();
        List<String> variableNames = extractVariables(expression);

        for (String var : variableNames) {
            feature.put(var, Double.valueOf(featureMap.getOrDefault(var, String.valueOf(Constant.MISSING_VALUE))));
        }

        System.out.println(evaluateCondition(expression, feature));


    }




}
